<!DOCTYPE html>
<html>
<head>
  <title>Welcome</title>
  <meta charset="utf-8"/>
  <!-- 引入 vue.js -->
  <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  </head>
  <body>

  <div id="app">
    <fly-table :fields="fields" :goods="goods">
      <span slot="title">Fly Table Component</span>
    </fly-table>
  </div>
  <script type="text/javascript">
    Vue.component('fly-table',{
      props: {
        fields:{
          type: Array,
          default(){
            return [];
          }
        },
        goods:{
          type: Array,
          default() {
            return [];
          }
        }
      },
      methods: {
        reverse (){
          this.goods.reverse();
        }
      },
      render (createElement){
          return createElement('div',{
            slot: 'fly-table'
          },[createElement('h2',this.$slots.title),createElement('button',{class: ['btn','btn-text'],
            attrs: {disabled:false, title:'点击是数组倒序'},
            domProps:{innerText:'倒序'},
            on:{
              click:() =>{
                this.goods.reverse()
              }
            },
            directives: [],
            key: 'btnReverse',
            ref: 'btnRevers'}),createElement('table',{
              style:{
                width: '400px',
                textAlign: 'left',
                lineHeight: '42px',
                border: '1px solid #eee',
                userSelect: 'none'
              }
          },[createElement('tr',[this.fields.map(field =>createElement('th',field.prop))]),
          this.goods.map(item => createElement('tr',{
            style: {
              color:item.isMarked? '#ea4335': ''
            }
          },this.fields.map(field =>createElement('td',{
            style:{
              borderTop: '1px solid #eee'
            }
          },[field.prop !== 'operate'?
                  createElement('span',item[field.prop])
                  : createElement('button',{
                    class: ['btn','btn-text'],
                    domProps: {
                      innerHTML:'<span>切换标记</span>'
                    },
                    on: {
                      click: () =>{
                        item.isMarked = !item.isMarked
                      }
                    }
                  })
          ]))))])])
      }
    });
    let vm = new Vue({
      el: '#app',
      data (){
        return {
          fields: [
            {
              label: '名称',
              prop: 'name'
            },
            {
              label: '数量',
              prop: 'quantity'
            },
            {
              label: '价格',
              prop: 'price'
            },
            {
              label: '',
              prop: 'operate'
            },
          ],
          goods: [
            {
              name: '苹果',
              quantity: 200,
              price: 6.8,
              isMarked: false
            },
            {
              name: '西瓜',
              quantity: 50,
              price: 4.8,
              isMarked: false
            },
            {
              name: '榴莲',
              quantity: 0,
              price: 22.8,
              isMarked: false
            },
          ]
        }
      }
    })
  </script>

  <style>
    .btn {
      outline: none;
      border: none;
      cursor: pointer;
      padding: 5px 12px;
    }
    .btn-text {
      color: #409eff;
      background-color: transparent;
    }
    .btn-text:hover {
      color: #66b1ff;
    }
  </style>

  </body>
  </html>

